package com.tokenplus.thai.earn.invite

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.adata.home.getInviteFriendEarn.ReqGetInviteFriendEarn
import com.tokenplus.thai.aviewmodel.HomeViewModel
import com.tokenplus.thai.databinding.ActivityInviteDetailBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.*
import com.tokenplus.thai.earn.invite.adapter.InviteDetailAdapter
import com.tokenplus.thai.home.repo.HomeCoinManager

/** 我的币页 */
@SuppressLint("CustomSplashScreen")
class InviteDetailActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityInviteDetailBinding

    //适配器
    private lateinit var mInviteDetailAdapter: InviteDetailAdapter

    //home viewModel
    private val mHomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mHomeViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.my_wallet.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityInviteDetailBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化刷新组件
            initRefreshUI()

            //监听返回
            onHandleLiveDataCallback()

            //初始化适配器
            mHomeViewModel.mGetInviteIncomeList.clear()
            mInviteDetailAdapter = InviteDetailAdapter(mHomeViewModel.mGetInviteIncomeList)

            mActivityBinding.listInviteDetail.layoutManager = LinearLayoutManager(this)
            mActivityBinding.listInviteDetail.adapter = mInviteDetailAdapter

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        //获取转入收益详情
        getInviteIncomeList()
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        mHomeViewModel.onGetInviteIncomeListLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mHomeViewModel.mGetInviteIncomeList.clear()
                    mHomeViewModel.mGetInviteIncomeList.addAll(result)
                    mInviteDetailAdapter.notifyDataSetChanged()

                    if (mHomeViewModel.mGetInviteIncomeList.size > 0) {
                        mActivityBinding.emptyLayout.llEmptyList.visibility = View.GONE
                    } else {
                        mActivityBinding.emptyLayout.llEmptyList.visibility = View.VISIBLE
                    }
                }
            }
        }
    }

    /** 获取转入收益详情 */
    private fun getInviteIncomeList() {
        HomeCoinManager.mGlobalCoinTypeLiveData.value?.let {
            //获取转入收益详情
            val reqGetInviteFriendEarn = ReqGetInviteFriendEarn(coinType = it)
            reqGetInviteFriendEarn.showLoading = false
            mHomeViewModel.getInviteIncomeList(reqGetInviteFriendEarn)
        }
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mActivityBinding.swipeRefresh) {
            //获取转入收益详情
            getInviteIncomeList()
        }
    }
}